using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AssetManagementSystem.Model;

namespace AssetManagementSystem.View.Controls {
    public partial class UIMain : Form {

        private const string ASSET_HEADING = "Asset";
        private const string STUDENT_HEADING = "Student";
        private const string LOAN_HEADING = "Loan";
        
        List<Student> studentList;
        List<Loan> selectedStudentsLoans;
        List<Asset> selectedLoansAssets;

        List<Asset> assetList;

        ControlAssets contAsset;
        ControlStudents contStudents;
        ControlLoans contLoans;

        private const string ALL_GROUPS = "*ALL*";

        //Public Events
        public event EventHandler Student_Selected;

        public event EventHandler Search_Clicked;
        public event EventHandler Save_Student;
        public event EventHandler AddStudent_Clicked;


        public UIMain() {
            InitializeComponent();
            studentList = new List<Student>();
            selectedStudentsLoans = new List<Loan>();
            assetList = new List<Asset>();
            
            //Add Tabs
            SetupStudentTab();
            SetupAssetTab();
            SetupLoanTab();
        }

        private void SetupStudentTab() {
            TabPage newPage = new TabPage(STUDENT_HEADING);
            contStudents = new ControlStudents();
            contStudents.Dock = DockStyle.Fill;
            newPage.Controls.Add(contStudents);
            tabMain.TabPages.Add(newPage);
        }

        private void SetupAssetTab() {
            TabPage newPage = new TabPage(ASSET_HEADING);
            contAsset = new ControlAssets();
            contAsset.Dock = DockStyle.Fill;
            newPage.Controls.Add(contAsset);
            tabMain.TabPages.Add(newPage);
        }

        private void SetupLoanTab() {
            TabPage newPage = new TabPage(LOAN_HEADING);
            contLoans = new ControlLoans();
            contLoans.Dock = DockStyle.Fill;
            newPage.Controls.Add(contLoans);
            tabMain.TabPages.Add(newPage);
        }



        private void UIMain_Load(object sender, EventArgs e) {

        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void studentToolStripMenuItem_Click(object sender, EventArgs e) {
            contStudents.btnAddStudent_Click(contStudents, e);
        }

        private void assetToolStripMenuItem_Click(object sender, EventArgs e) {
            contAsset.AddAsset_Click(contAsset, e);
        }

        private void tabMain_Selected(object sender, TabControlEventArgs e) {
            RefreshTab();
        }

        private void RefreshTab(){
            if (tabMain.SelectedTab.Text == ASSET_HEADING) {
                contAsset.RefreshAll();
            } else if (tabMain.SelectedTab.Text == STUDENT_HEADING) {
                contStudents.RefreshAll();
            } else if (tabMain.SelectedTab.Text == LOAN_HEADING) {
                contLoans.RefreshAll();
            }
        }

        private void loanToolStripMenuItem_Click(object sender, EventArgs e) {
            UILoanEditor loanEdit = new UILoanEditor();
            loanEdit.Show();
            loanEdit.FormClosing += new FormClosingEventHandler(loanEditorClosed);
        }

        private void loanEditorClosed(object sender, EventArgs e) {
            RefreshTab();
        }

        private void notificationsToolStripMenuItem_Click(object sender, EventArgs e) {
            UIEmail uiEmail = new UIEmail();
            uiEmail.Show();
        }

        private void holdToolStripMenuItem_Click(object sender, EventArgs e) {
            UIHoldEditor holdEditor = new UIHoldEditor();
            holdEditor.Show();
        }
    }
}
